﻿package de.gameduell.framework.game.card {
	import de.gameduell.framework.game.card.Card;
	import de.gameduell.framework.util.Library;	

	/**
	 * @author dkum
	 */
	public class MultipleFaceCard extends Card {

		private var coverString:String;
		private var highlightString:String;
		private var cardPrefixes:Array;
		private var cardClips:Array = new Array();
		private var coverClips:Array = new Array();
		private var highlightClips:Array = new Array();

		public function MultipleFaceCard(aCard:String, theCardPrefixes:Array, aCover:String = "", aHighlight:String = "") {
			cardPrefixes = theCardPrefixes;
			coverString = aCover;
			highlightString = aHighlight;
			var defaultCardPrefix:String = cardPrefixes[0];
			var defaultCover:String = aCover.length > 0 ? defaultCardPrefix + aCover : aCover;
			var defaultHighlight:String = aHighlight.length > 0 ? defaultCardPrefix + aHighlight : aHighlight;
			super(aCard, defaultCover, defaultHighlight, defaultCardPrefix);
			buildFaces();
		}

		private function buildFaces():void {
			var library:Library = Library.getInstance();
			cardClips[cardPrefixes[0]] = cardClip;
			coverClips[cardPrefixes[0]] = coverClip;
			highlightClips[cardPrefixes[0]] = _highlightClip;
			for (var i:int = 1;i < cardPrefixes.length;i++) {
				var prefix:String = cardPrefixes[i];
				cardClips[prefix] = library.createSprite(prefix + cardString); 
				cardClips[prefix].visible = false;
				addChild(cardClips[prefix]);
				if (coverString.length > 0) {
					coverClips[prefix] = library.createSprite(prefix + coverString); 
					coverClips[prefix].visible = false;
					addChild(coverClips[prefix]);
				}
				if (highlightString.length > 0) {
					highlightClips[prefix] = library.createSprite(prefix + highlightString); 
					highlightClips[prefix].visible = false;
					addChild(highlightClips[prefix]);
				}
//				cardClip = cardClips[prefix];
			}
		}

		public function changeFace(newPrefix:String):void {
			cardClip = cardClips[newPrefix];
			cardClip.visible = true;
			if (coverString.length > 0) {
				var currentFlip:Boolean = flip;
				coverClip = coverClips[newPrefix];
				coverClip.visible = currentFlip;
			}
			if (highlightString.length > 0) {
				_highlightClip = highlightClips[newPrefix];
			}
			for (var i:int = 0;i < cardPrefixes.length;i++) {
				var prefix:String = cardPrefixes[i];
				if (prefix != newPrefix) {
					cardClips[prefix].visible = false;
					if (coverString.length > 0) {
						coverClips[prefix].visible = false;
					}
					if (highlightString.length > 0) {
						highlightClips[prefix].visible = false;
					}
				}
			}
		}

	}
}
